// pages/upimg/upimg.js
const wxutil = require("../../utils/wxutil.js");

//getApp()的数据
let app = getApp();
let gd = app.globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: [],
    ind: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     
     
    this.setData({
      ind: options.ind,
    })
    if (gd.orederMes[this.data.ind-0].upimg) {
      this.setData({
        phone: gd.orederMes[this.data.ind-0].upimg
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  // 拍照
  getphone() {
    let phoneBox = this.data.phone;
    if (phoneBox.length >= 3) {
      wxutil.toast("最多拍摄三张照片");
      return;
    }
    wx.showLoading({
      title: "正在生成图片",
      masK: true
    })
    const camera = wx.createCameraContext();
    camera.takePhoto({
      quality: "normal",
      success: (res1) => {
        let tempImagePath = res1.tempImagePath;
        wx.saveFile({
          tempFilePath: tempImagePath,
          success: (res2) => {
            let saveimg = res2.savedFilePath;
            wx.uploadFile({
              url: "https://callme.lixinapp.com/weixiu/api/addimg",
              filePath: saveimg + "",
              name: "file",
              formData: {},
              success: (res3) => {
                const data = JSON.parse(res3.data);
                if (data.result == 0) {
                  phoneBox.push(data.object)
                  this.setData({
                    phone: phoneBox
                  })
                  wx.hideLoading();
                }
              }
            })
          }
        })


      }
    })

    // wx.chooseImage({
    //   sourceType: "camera",
    //   success(res) {
    //     const tempFilePaths = res.tempFilePaths
    //     wx.uploadFile({
    //       url: 'https://callme.lixinapp.com/weixiu/api/addimg', // 仅为示例，非真实的接口地址
    //       filePath: tempFilePaths[0],
    //       name: 'file',
    //       success(res) {
    //          
    //         // do something
    //       }
    //     })
    //   }
    // })
  },
  // 删除照片
  removephone(event) {
    let ind = event.currentTarget.dataset.index;
    let phone = this.data.phone;
    phone.splice(ind, 1)
    this.setData({
      phone: phone
    })

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    gd.orederMes[this.data.ind-0].upimg = this.data.phone;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})